#include <iostream>

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"

using namespace cv;
using namespace std;

void drawText(Mat& image);

int main() {
  cout << "Built with OpenCV " << CV_VERSION << endl;
  Mat image;
  Mat image_hsv;
  Mat image_gray;
  VideoCapture capture;
  bool first_capture_save = true;
  capture.open(0);
  if (capture.isOpened()) {
    cout << "Capture is opened" << endl;
    for (;;) {
      capture >> image;
      if (image.empty()) break;
      cvtColor(image, image_hsv, COLOR_BGR2HSV);    // to hsv 图
      cvtColor(image, image_gray, COLOR_BGR2GRAY);  // to 灰度图
      drawText(image);
      imshow("origin", image);
      drawText(image_hsv);
      imshow("hsv", image_hsv);
      drawText(image_gray);
      imshow("gray", image_gray);
      imwrite("/home/linux/Pictures/input.png", image);
      imwrite("/home/linux/Pictures/hsv.png", image_hsv);
      imwrite("/home/linux/Pictures/gray.png", image_gray);
      if (waitKey(10) >= 0) {
        break;
      }
    }
  } else {
    cout << "No capture" << endl;
    image = Mat::zeros(480, 640, CV_8UC1);
    drawText(image);
    imshow("Sample", image);
    waitKey(0);
  }
  return 0;
}

void drawText(Mat& image) {
  putText(image, "Hello OpenCV", Point(20, 50), FONT_HERSHEY_COMPLEX,
          1,                      // font face and scale
          Scalar(255, 255, 255),  // white
          1, LINE_AA);            // line thickness and type
}
